package com.jiexinyun.dsl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.Data;

@Data
public class SafeQueryDSL<T> {

    //留做备用
//    private T t;

    private LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
    private Map<String, SFunction<T, ?>> columnMappings;

//    public static void main(String[] args) {
//        // 先定义列名映射
//        Map<String, Function<UsersEntity, ?>> userColumns = new HashMap<>();
//        userColumns.put("name", UsersEntity::getName);
//        userColumns.put("age", UsersEntity::getAge);
//        userColumns.put("createTime", UsersEntity::getCreateTime);
//
//// 使用DSL
//        LambdaQueryWrapper<UsersEntity> wrapper = SafeQueryDSL.create(UsersEntity.class, userColumns)
//                .where("name").like("张")
//                .where("age").eq(25)
//                .build();
//    }

    public SafeQueryDSL() {
        new SafeQueryDSL<Long>(Long.class, null);
    }

    public SafeQueryDSL(Class<T> entityClass, Map<String, SFunction<T, ?>> columnMappings) {
        this.columnMappings = columnMappings;
    }

    public static <T> SafeQueryDSL<T> create(Class<T> entityClass,
                                             Map<String, SFunction<T, ?>> columnMappings) {
        return new SafeQueryDSL<>(entityClass, columnMappings);
    }

    public ColumnDSL where(String column) {
        if (!columnMappings.containsKey(column)) {
            throw new IllegalArgumentException("Unknown column: " + column);
        }
        return new ColumnDSL(column);
    }

    public class ColumnDSL {
        private final String column;

        public ColumnDSL(String column) {
            this.column = column;
        }

        //相等，一致
        public SafeQueryDSL<T> eq(Object value) {
            if (value != null) {
                wrapper.eq(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //模糊查询
        public SafeQueryDSL<T> like(Object value) {
            if (value != null) {
                wrapper.like(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //大于
        public SafeQueryDSL<T> gt(Object value) {
            if (value != null) {
                wrapper.gt(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //小于
        public SafeQueryDSL<T> lt(Object value) {
            if (value != null) {
                wrapper.lt(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //大于等于
        public SafeQueryDSL<T> ge(Object value) {
            if (value != null) {
                wrapper.ge(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //小于等于
        public SafeQueryDSL<T> le(Object value) {
            if (value != null) {
                wrapper.le(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //大于等于
        public SafeQueryDSL<T> in(Object value) {
            if (value != null) {
                wrapper.in(columnMappings.get(column), value);
            }
            return SafeQueryDSL.this;
        }

        //大于等于
        public SafeQueryDSL<T> between(Object value1, Object value2) {
            if (value1 != null && value2 != null) {
                wrapper.between(columnMappings.get(column), value1 ,value2);
            }
            return SafeQueryDSL.this;
        }


        // 其他比较方法...
    }

}
